#include "castle.h"
#include "terrain.h"
#include "texture1.h"
#include "key.h"
#include "draw.h"
#include "Vec3.h"
#include "FrustumG.h"
#include "definitions.h"
#include "toolkits/includes/glut.h"
#include "toolkits/includes/glm.h"
#include "toolkits/includes/il.h"

extern GLfloat currentColor[3];
extern unsigned int texID;
extern float castlePosition[3];
extern GLMmodel *castle;
extern Key *keyClass;
extern Terrain *terrainClass;
extern float heightMap[TERRAINLENGTH*2][TERRAINLENGTH*2];
extern ILint heightH, heightW; 
extern Texture1 *textureClass;
extern int castleDL;
extern FrustumG frustum;
extern bool frustumOn;

Castle::Castle() {
}

void Castle::drawCastle() {
	float x = 0.0f, y = 0.0f, z = -(TERRAINLENGTH-20);
	y = CASTLESCALEFACTOR + heightMap[(int)x+TERRAINLENGTH][(int)z+TERRAINLENGTH];
	//y = CASTLESCALEFACTOR + heightMap[(int)x][(int)z] - 1.0f;
	Vec3 a(x,y,z);
	if (!frustumOn || (frustum.sphereInFrustum(a,CASTLESCALEFACTOR) != FrustumG::OUTSIDE)) {//printf("SIM\n");
		glPushMatrix();
			Draw::setColor(0.4f, 0.0f, 0.4f);
			glTranslatef(x, y-1.0f, z);
			glScalef(CASTLESCALEFACTOR, CASTLESCALEFACTOR, CASTLESCALEFACTOR);
	
			glmDraw(castle, GLM_MATERIAL | GLM_SMOOTH);

			updateCastlesPosition(x, y, z);
			if(keyClass->calcCapturedKeys() == KEYS) 
				drawTreasure();
		glPopMatrix();
		glColor3fv(currentColor);
	}//else printf("NAO\n");
}

void Castle::drawTreasure() {
	glTranslatef(0.0f, -0.4f, 0.0f);
	Draw::setColor(0.0f, 1.0f, 0.0f);
	glutSolidTeapot(0.5); // treasure
	glColor3fv(currentColor);
}

void Castle::updateCastlesPosition(float x, float y, float z) {
	castlePosition[0] = x;
	castlePosition[1] = y;
	castlePosition[2] = z;
}
